home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / pccstdio.lzh / SRC.LZH / SFLNME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-05-17  |  2.1 KB  |  101 lines

  1. /*    sflnme.cl - set file name from string and default.
  2.     (C) Copyright 1983 Gregory R. Mansfield - All Rights Reserved.
  3.     G. R. Mansfield.  83/12/03.
  4.     Ver 1.1-5517.
  5. */
  6.  
  7. #include "defstd.h"
  8. #include "ctype.h"
  9.  
  10. #define L_DEV 3
  11. #define L_NAME 8
  12. #define L_TYP 3
  13.  
  14. /* local functions */
  15. static void cpyfld(), pflnme();
  16.  
  17. sflnme(n, s, d)  /* set file name n to s OR d */
  18. char *n, *s, *d;
  19. /* Set file name n to s.  If device, name, or type fields in s are not
  20.    specified, set them to the corresponding fields of d (the default
  21.    name).
  22. */
  23. {
  24.     char dev[L_DEV+1];
  25.     char nme[L_NAME+1];
  26.     char typ[L_TYP+1];
  27.     char *p;
  28.  
  29.     *dev = *nme = *typ = 0;
  30.     dev[L_DEV] = nme[L_NAME] = typ[L_TYP] = 0;
  31.     pflnme(d, dev, nme, typ);
  32.     if (*s) {
  33.         if (strlen(dev) > 1)
  34.             *dev = 0;
  35.         pflnme(s, dev, nme, typ);
  36.     }
  37.     /* Merge device, name and type to name string with ':' and '.' */
  38.     if (*dev) {
  39.         for (p = dev; *p; p++)
  40.             *n++ = toupper(*p);
  41.         *n++ = ':';
  42.     }
  43.     if (strlen(dev) < 2) {
  44.         for (p = nme; *p; p++)
  45.             *n++ = toupper(*p);
  46.         if (*typ) {
  47.             *n++ = '.';
  48.             for (p = typ; *p; p++)
  49.                 *n++ = tolower(*p);
  50.         }
  51.     }
  52.     *n = 0;
  53. }
  54.  
  55. static void pflnme(s, dev, nme, typ)    /* parse file name */
  56. char *s, *dev, *nme, *typ;
  57.  
  58. /* Extract device, name, and type from string to dev, name, typ.
  59.    If device or type are missing, dev or typ are unchanged.
  60.    If device or type are blank, dev or typ are set empty.
  61.    If name is blank, name is not changed.
  62. */
  63. {
  64.     char *p;
  65.     int i;
  66.  
  67.     if ((p = index(s, ':')) != NULL) {    /* process device. */
  68.         if (i = p - s)
  69.             cpyfld(dev, s, min(i, L_DEV));
  70.         else
  71.         *dev = '\0';
  72.         s += ++i;
  73.     }
  74.  
  75.     if ((p = index(s, '.')) == NULL)    /* process name */
  76.         i = strlen(s);
  77.     else
  78.     i = p - s;
  79.     if (i)
  80.         cpyfld(nme, s, min(i, L_NAME));
  81.  
  82.     if (p != NULL) {    /* process type. */
  83.         ++p;
  84.         if ((i = min(strlen(p), L_TYP)) > 0)
  85.             cpyfld(typ, p, i);
  86.         else
  87.         *typ = '\0';
  88.     }
  89. }
  90.  
  91. static void cpyfld(s1, s2, n) /* copy exactly n characters from s2 to s1 */
  92. char *s1, *s2;
  93. int n;
  94. {
  95.     char *p;
  96.     p = s1;
  97.     while (n-- && (*p++ = *s2++))
  98.         ;
  99.     *p++ = '\0';
  100. }
  101.